package jmine.tec.script.persistent.mock;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

import jmine.tec.script.persistent.context.impl.AbstractScriptExecutionContext;

/**
 * Contexto de execução de script mock.
 * 
 * @author lundberg
 */
public class MockValidationScriptContextImpl extends AbstractScriptExecutionContext<String> implements MockValidationScriptContext {

    private static final String INPUT = "input";

    private static final String OUTPUT = "output";

    /**
     * {@inheritDoc}
     */
    public MockValidationScriptContext prepare(String input) {
        try {
            this.getParameters().putParameter(INPUT, new ByteArrayInputStream(input.getBytes("UTF-8")));
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void beforeExecution() {
        super.beforeExecution();
        this.getParameters().putParameter(OUTPUT, new ByteArrayOutputStream());
    }

    /**
     * {@inheritDoc}
     */
    public String getResult() {
        try {
            return new String(((ByteArrayOutputStream) this.getParameters().getParameterValues().get(OUTPUT)).toByteArray(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * {@inheritDoc}
     */
    public String getTracer() {
        return null;
    }
}
